Skip to content

Conversation

Copy link

Copilot AI commented Feb 1, 2026

Adds single command to retrieve database health and configuration info, consolidating what previously required running wp db size, wp db tables, wp db prefix, and wp db check separately.

Implementation

  • New status() method in DB_Command class

    • Queries information_schema.TABLES for size, engine, charset, collation
    • Detects mixed engines/charsets/collations across tables
    • Runs mysqlcheck for health status
    • Formats output as aligned key-value pairs
    • Supports --dbuser, --dbpass, and --defaults options for authentication
  • Command registration in composer.json

  • Behat test coverage for output validation and credential options

Output

Database Name:     wp_cli_test
Tables:            54
Total Size:        312 KB
Prefix:            wp_
Engine:            InnoDB
Charset:           utf8mb4
Collation:         utf8mb4_unicode_ci
Check Status:      OK

Notes

  • Uses SI units (1000-based) for size formatting, matching existing wp db size --human-readable behavior
  • Handles edge cases: zero/null size, missing tables, check failures, mixed engines/charsets/collations
  • Works without loading WordPress (@when before_wp_load), suitable for automation and use before WordPress is installed
  • Supports standard database authentication options (--dbuser, --dbpass, --defaults) for consistency with other db commands
Original prompt

This section details on the original issue you should resolve

<issue_title>Add wp db status command for quick database health overview</issue_title>
<issue_description>I would like to suggesta new WP-CLI command: wp db status

The command would provide a concise, read-only overview of the current WordPress database health and configuration in a single output, reducing the need to run multiple separate commands.

For support, debugging, and hosting diagnostics, it would be beneficial to have a single, non-destructive command that aggregates key database information into one quick report.

Currently, checking database status requires running several commands such as: wp db size, wp db tables, wp db prefix, wp db check, etc.

My Proposed Command:

wp db status

Output Example:

Database Name:     wp_mehrazmorshed
Tables:            54
Total Size:        312 MB
Prefix:            wp_
Engine:            InnoDB
Charset:           utf8mb4
Collation:         utf8mb4_unicode_ci
Check Status:      OK
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@ernilambar</author><body>
It will be included in next "3.0.0" version.</body></comment_new>
<comment_new><author>@swissspidy</author><body>
Yeah you can already try it out in the nightly release.

One difference is that the site-health command involves loading WordPress, whereas the db commands usually also work when WordPress is not even installed yet.</body></comment_new>
<comment_new><author>@ernilambar</author><body>
Although database info is available from "site-health" command I am positive is adding specific db command which gives info without loading whole WordPress. It would be helpful in scripting.</body></comment_new>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 1, 2026 16:10
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add wp db status command for database health overview Add wp db status command for database health overview Feb 1, 2026
Copilot AI requested a review from swissspidy February 1, 2026 16:13
@codecov
Copy link

codecov bot commented Feb 1, 2026

Codecov Report

❌ Patch coverage is 98.03922% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/DB_Command.php 98.03% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy requested a review from Copilot February 2, 2026 01:50
@swissspidy

This comment was marked as resolved.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new wp db status command, which provides a convenient overview of the database's health and configuration. The implementation is solid and consolidates information from several other commands. My review focuses on improving the new Behat tests for better robustness and refactoring parts of the new status() method to enhance code clarity, reduce duplication, and improve maintainability.

Comment on lines +3 to +56
Scenario: Display database status for a WordPress install
Given a WP install

When I run `wp db status`
Then STDOUT should contain:
"""
Database Name:
"""
And STDOUT should contain:
"""
Tables:
"""
And STDOUT should contain:
"""
Total Size:
"""
And STDOUT should contain:
"""
Prefix: wp_
"""
And STDOUT should contain:
"""
Engine:
"""
And STDOUT should contain:
"""
Charset:
"""
And STDOUT should contain:
"""
Collation:
"""
And STDOUT should contain:
"""
Check Status:
"""

Scenario: Verify database status shows correct database name
Given a WP install

When I run `wp db status`
Then STDOUT should contain:
"""
wp_cli_test
"""

Scenario: Verify database status shows check status as OK
Given a WP install

When I run `wp db status`
Then STDOUT should contain:
"""
Check Status: OK
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Behat tests are split across multiple scenarios, each testing a small part of the command's output. This makes the tests less robust as they don't verify the output structure as a whole. Combining these into a single, more comprehensive scenario would improve test clarity and strength. Using should match with regular expressions for dynamic values like table count and size will also make the assertions more precise and resilient to minor changes.

  Scenario: Display database status for a WordPress install
    Given a WP install

    When I run `wp db status`
    Then STDOUT should contain:
      """
      Database Name:     wp_cli_test
      """
    And STDOUT should match /^Tables:\s+\d+$/m
    And STDOUT should match /^Total Size:\s+[\d.]+ \wB$/m
    And STDOUT should contain:
      """
      Prefix:            wp_
      """
    And STDOUT should match /^Engine:\s+\w+$/m
    And STDOUT should match /^Charset:\s+\w+$/m
    And STDOUT should match /^Collation:\s+\w+$/m
    And STDOUT should contain:
      """
      Check Status:      OK
      """

Comment on lines 1290 to 1298
if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) {
$db_size = '0 B';
} else {
$size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) );
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];
$divisor = pow( 1000, $size_key );
$db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block for formatting the database size into a human-readable string is very similar to logic within the size() method. To avoid code duplication and improve maintainability, consider extracting this logic into a private helper function that can be reused in both places. For now, the logic can be slightly simplified for better readability by inverting the conditional and using the null coalescing operator.

		if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) {
			$size_key    = floor( log( (float) $db_size_bytes ) / log( 1000 ) );
			$sizes       = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
			$size_format = $sizes[ $size_key ] ?? $sizes[0];
			$divisor     = pow( 1000, $size_key );
			$db_size     = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format;
		} else {
			$db_size = '0 B';
		}

Comment on lines +1339 to +1346
WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', $db_name ) );
WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) );
WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) );
WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) );
WP_CLI::log( sprintf( '%-18s %s', 'Engine:', $engine ) );
WP_CLI::log( sprintf( '%-18s %s', 'Charset:', $charset ) );
WP_CLI::log( sprintf( '%-18s %s', 'Collation:', $collation ) );
WP_CLI::log( sprintf( '%-18s %s', 'Check Status:', $check_status ) );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The block of WP_CLI::log() calls for outputting the status is repetitive. This can be refactored to be data-driven by storing the status information in an associative array and then iterating over it to generate the output. This approach is more maintainable and makes it easier to add or modify status fields in the future.

		$status_items = [
			'Database Name' => $db_name,
			'Tables'        => $table_count,
			'Total Size'    => $db_size,
			'Prefix'        => $prefix,
			'Engine'        => $engine,
			'Charset'       => $charset,
			'Collation'     => $collation,
			'Check Status'  => $check_status,
		];

		foreach ( $status_items as $label => $value ) {
			WP_CLI::log( sprintf( '%-18s %s', $label . ':', $value ) );
		}

This comment was marked as resolved.

swissspidy and others added 3 commits February 2, 2026 20:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add wp db status command for quick database health overview

2 participants